package com.jzsm.common.utils.file;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.jzsm.common.utils.ScmUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 文件存储处理工具类
 *
 * @author ruoyi
 */
@Component
public class FileSaveUtils {
    // word报表
    public final static String WORD_PATH = "word";
    // pdf报表
    public final static String PDF_PATH = "pdf";
    // excel 报表
    public final static String EXCEL_PATH = "excel";
    // xml( 用于存放数据源的 sql )
    public final static String XML_PATH = "xml";

    // 临时路径
    public final static String TEMP_PATH = "Temp";

    /**
     * 存储目录
     */
    private static String basedir;

    @Value("${server.tomcat.basedir}")
    public void setBasedir(String basedir) {
        FileSaveUtils.basedir = basedir;
    }

    /**
     * 获取xml文件存储路径
     *
     * @param
     * @return
     */
    public static String getMapperXmlSaveDir() {
        String path = getSaveDir("/" + XML_PATH);
        if (!FileUtil.exist(path)) {
            FileUtil.mkdir(path);
        }
        return path;
    }

    /**
     * 获取前缀
     *
     * @return
     */
    private static String getSaveDir(String path) {
        String groupId = String.valueOf(ScmUtil.getGroupId());
        return basedir + "/GroupId_" + groupId + path;
    }

    /**
     * 获取当前时间路径
     *
     * @return
     */
    private static String getDatePath() {
        DateTime now = DateUtil.date();
        return DateUtil.format(now, "yyyy/MM/dd");
    }

    /**
     * 获取本地保存路径
     *
     * @return
     */
    private static String getSaveDirByDocType(String docType) {
        // 目录
        String localPath = getSaveDir("/" + docType);
        if (!FileUtil.exist(localPath)) {
            FileUtil.mkdir(localPath);
        }
        return localPath;
    }

    /**
     * 获取 word 文档保存路径
     *
     * @return
     */
    public static String getWordSaveDir() {
        return getSaveDirByDocType(WORD_PATH);
    }

    /**
     * 获取 excel 文档保存路径
     *
     * @return
     */
    public static String getExcelSaveDir() {
        return getSaveDirByDocType(EXCEL_PATH);
    }

    /**
     * 获取 pdf 文档保存路径
     *
     * @return
     */
    public static String getPdfSaveDir() {
        return getSaveDirByDocType(PDF_PATH);
    }

    /**
     * 获取 临时 文档保存路径
     *
     * @return
     */
    public static String getTempSaveDir() {
        return getSaveDirByDocType(TEMP_PATH + "/" + getDatePath());
    }

}
